<template>
	<view class="route-editaddress">
		<view class="content">
			<view class="form">
				<u--form labelWidth="85" :labelStyle="{fontSize:'14px',color:'#2E2D2D'}" labelAlign="left"
					labelPosition="left" :model="formValues" ref="formRef">
					<u-form-item label="收货人:" prop="name">
						<u--input type="text" v-model="formValues.name" border="none" placeholder="请输入"></u--input>
					</u-form-item>
					<u-form-item label="手机号:" prop="phone">
						<u--input type="text" v-model="formValues.phone" border="none" placeholder="请输入"></u--input>
					</u-form-item>
					<u-form-item label="地址:" prop="area" @click="handleGoSelectMap">
						<u--input class="myinput" v-model="formValues.area" disabled disabledColor="#ffffff"
							placeholder="请选择地址" border="none"></u--input>
						<template #right>
							<u-icon name="arrow-right"></u-icon>
						</template>
					</u-form-item>
					<u-form-item label="门牌号:" prop="menpai">
						<u--input type="text" v-model="formValues.menpai" border="none" placeholder="请输入"></u--input>
					</u-form-item>


				</u--form>
			</view>
			<!-- <view class="default">
				<view class="">
					设为默认地址
				</view>
				<view class="">
					<u-switch v-model="isDefault" size="20" activeColor="#ED2920"></u-switch>
				</view>
			</view> -->

		</view>
		<view class="footer">
			<view class="btn" @click="handleSubmit">
				保存
			</view>
		</view>
	</view>
</template>

<script setup>
	import handleCache from "@/utils/cache.js"
	import {

		addressApi
	} from '../../api';
	import {
		ref,
		reactive,
		getCurrentInstance
	} from 'vue'
	import {
		onReady,
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const {
		proxy
	} = getCurrentInstance();
	const isDefault = ref(false)
	const userInfo = ref('')
	const addressId = ref('')
	const addressInfo = ref('')
	const type = ref('')
	let formValues = reactive({
		name: '',
		phone: '',
		menpai: '',
		area: ''
	})
	let areainfo = reactive({
		address: '',
		location: {}
	})
	onShow(() => {
		userInfo.value = handleCache.get('userInfo')
	})
	onLoad((options) => {
		if (options.id) {
			addressId.value = Number(options.id)
			getAddressInfo()
		}
		if (options.type) {
			type.value = options.type
		}
	})
	onReady(() => {
		const rules = {

			name: [{
				type: 'string',
				required: true,
				message: '请输入',
			}],
			phone: [{
				type: 'string',
				required: true,
				message: '请输入',
			}],
			area: [{
				type: 'string',
				required: true,
				message: '请选择地址',
			}],
			menpai: [{
				type: 'string',
				required: true,
				message: '请输入',
			}],


		}
		proxy.$refs.formRef.setRules(rules)
	})
	const setAreaInfo = (data) => {

		areainfo = data
		formValues.area = data.address

	}
	const getAddressInfo = async () => {
		let {
			data
		} = await addressApi.userareaInfo({
			id: addressId.value
		})
		addressInfo.value = data
		formValues.name = data.name
		formValues.phone = data.phone
		formValues.menpai = data.menpai
		formValues.area = data.area
		areainfo.address = data.area
		areainfo.location = {
			lat: data.lat,
			lng: data.lng
		}

	}
	const handleGoSelectMap = () => {
		uni.navigateTo({
			url: '/pages/selectMap/selectMap'
		})
	}
	const handleSubmit = () => {
		proxy.$refs.formRef.validate().then(() => {
			console.log(formValues, areainfo)
			let req = {
				uid: userInfo.value.id,
				name: formValues.name,
				phone: formValues.phone,
				area: formValues.area,
				menpai: formValues.menpai,
				lng: areainfo.location.lng.toString(),
				lat: areainfo.location.lat.toString()
			}
			if (!addressId.value) {

				addressApi.userAdderss(req).then((res) => {
					if (res.code === 0) {
						uni.showToast({
							icon: 'none',
							title: '保存成功',
							mask: true,

							complete: () => {
								uni.navigateBack(-1)
							}
						})
					}
				})
			} else {
				req.id = addressId.value
				addressApi.userAdderssupdate(req).then((res) => {
					if (res.code === 0) {
						uni.showToast({
							icon: 'none',
							title: '保存成功',
							mask: true,
							complete: () => {
								uni.navigateBack(-1)
							}
						})

					}
				})
			}
		})
	}
	defineExpose({
		setAreaInfo
	})
</script>

<style lang="scss" scoped>
	.route-editaddress {
		padding-bottom: calc(140rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(140rpx + env(safe-area-inset-bottom));

		.myinput {}

		.content {
			padding: 30rpx 24rpx 0 24rpx;

			.form {
				background-color: #fff;
				border-radius: 10rpx;
				padding: 0 30rpx;
			}

			.default {
				background-color: #fff;
				margin-top: 20rpx;
				border-radius: 10rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 24rpx;

			}
		}

		.footer {
			height: calc(100rpx + constant(safe-area-inset-bottom));
			height: calc(100rpx + env(safe-area-inset-bottom));
			position: fixed;
			width: 100%;
			left: 0;
			bottom: 0;
			z-index: 10;
			background-color: #fff;
			border-top: 1px solid #edf0f2;
			padding: 11rpx 24rpx 0 24rpx;

			.btn {
				width: 100%;
				height: 78rpx;
				background-color: #ED2920;
				text-align: center;
				line-height: 78rpx;
				color: #fff;
				border-radius: 39rpx;
			}
		}
	}
</style>